﻿
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BanXeTang
{
    public class MenuItem : Dialog
    {
        private Texture2D _texture2dClick;
        private Texture2D _texture2dMouseOver;
        private Texture2D _texture2dNormal;
        private int _index;
        public float xMax;

        protected MouseState lastMouseState;
        protected MouseState mouseState = Mouse.GetState();

        public Texture2D Texture2DClick
        {
            get { return _texture2dClick; }
            set { _texture2dClick = value; }
        }

        public Texture2D Texture2DMouseOver
        {
            get { return _texture2dMouseOver; }
            set { _texture2dMouseOver = value; }
        }

        public Texture2D Texture2DNormal
        {
            get { return _texture2dNormal; }
            set { _texture2dNormal = value; }
        }

        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }


        public override Dialog Clone()
        {
            Dialog newObject = new MenuItem();
            newObject._sprite = this._sprite;
            newObject._nsprite = this._nsprite;
            newObject.X = this.X;
            newObject.Y = this.Y;
            ((MenuItem)newObject).xMax = this.xMax;
            ((MenuItem)newObject).Texture2DNormal = this._texture2dNormal;
            ((MenuItem)newObject).Texture2DMouseOver = this._texture2dMouseOver;
            ((MenuItem)newObject).Texture2DClick = this._texture2dClick;
            ((MenuItem)newObject).Index = this._index;


            return newObject;
        }
        public override bool Init(ContentManager Content, string strResource)
        {
            _nsprite = 1;
            _sprite = new MySprite[_nsprite];
            Texture2D[] texture2D = new Texture2D[1];
            _texture2dNormal = Content.Load<Texture2D>(@"Pictures/" + strResource);
            _texture2dMouseOver = Content.Load<Texture2D>(@"Pictures/" + strResource + "_Mouseover");
            _texture2dClick = Content.Load<Texture2D>(@"Pictures/" + strResource + "_Click");
            texture2D[0] = _texture2dNormal;
            _sprite[0] = new MySprite(texture2D, X, Y);
            return true;
        }

        public override void Update(GameTime gameTime)
        {
            int viTriHighLightTruoc = Game1.viTriHighLight;
            int viTriChonTruoc = Game1.viTriChon;
            Input.lastKeyboardState = Input.keyboardState;
            Input.keyboardState = Keyboard.GetState();
            lastMouseState = mouseState;
            mouseState = Mouse.GetState();

            if (X < xMax)
                X += 2;

            if (Input.lastKeyboardState.IsKeyDown(Keys.Down) && Input.keyboardState.IsKeyUp(Keys.Down) && Game1.viTriHighLight < Game1._nMenuItemHienTai - 1)
            {
                Game1.viTriHighLight++;
                Game1.viTriChon = -1;
            }
            if (Input.lastKeyboardState.IsKeyDown(Keys.Up) && Input.keyboardState.IsKeyUp(Keys.Up) && Game1.viTriHighLight > 0)
            {
                Game1.viTriHighLight--;
                Game1.viTriChon = -1;
            }

            if (Input.keyboardState.IsKeyDown(Keys.Enter) && Game1.viTriHighLight >= 0)
                Game1.viTriChon = Game1.viTriHighLight;


            if (this.LeftClick())
            {
                _sprite[0].texture2d[0] = _texture2dClick;
                Game1.viTriHighLight = _index;
                Game1.viTriChon = _index;
            }
            else
            {
                if (this.MouseOver() && (mouseState.X != lastMouseState.X || mouseState.Y != lastMouseState.Y))
                {
                    Game1.viTriHighLight = _index;
                    if (Game1.viTriHighLight != Game1.viTriChon)
                        Game1.viTriChon = -1;
                    _sprite[0].texture2d[0] = _texture2dMouseOver;

                }
                else
                    _sprite[0].texture2d[0] = _texture2dNormal;
            }
            if (Game1.viTriHighLight != viTriHighLightTruoc && AmThanh.moAmThanh)
                AmThanh.drip.Play();
            if (Game1.viTriChon != viTriChonTruoc && AmThanh.moAmThanh)
                AmThanh.laser.Play();
            if (_index == Game1.viTriChon)
                _sprite[0].texture2d[0] = _texture2dClick;
            else
                if (_index == Game1.viTriHighLight && !this.LeftClick())
                    _sprite[0].texture2d[0] = _texture2dMouseOver;


        }

        public bool MouseOver()
        {
            return mouseState.X < this.X + _sprite[0].texture2d[0].Width && mouseState.X > this.X &&
                   mouseState.Y > this.Y && mouseState.Y < this.Y + _sprite[0].texture2d[0].Height;
        }
        public bool LeftClick()
        {
            return (MouseOver() && mouseState.LeftButton == ButtonState.Pressed);
        }
    }
}
